// packageI/invoice/invoiceCenter/invoiceCenter.js
const app = getApp();
Page({
  data: {
    invoiceType: 1,
    loading: false,
    finshed: false,
    page: 1,
    total: 0,
    invoiceList: [], // 发票列表
    orderList: [], // 订单列表
    bearerList: [], // 发票抬头列表

    sinvoice: {},
    balance_invoice: 0
  },
  onLoad: function (options) {
    if (options.type !== undefined)
      this.setData({
        invoiceType: options.type * 1
      })
  },

  onShow: function () {
    this._initData();
    this.getListData();
  },

  changeTab(event) {
    // 禁止初始化重复触发
    if (event.detail.name != this.data.invoiceType) {
      this.setData({
        invoiceType: event.detail.name
      })
      this._initData();
      this.getListData();
    }
  },
  updateData() {
    this._initData();
    this.setData({
      invoiceType: 5
    })
    this.getListData();
  },
  _initData() {
    this.setData({
      loading: false,
      finshed: false,
      page: 1,
      total: 0,
      invoiceList: [],
      orderList: [],
      bearerList: [],
    })
  },

  getListData() {
    if (this.data.finshed || this.data.loading) {
      return false;
    }
    let urlStr = "";
    let requestJson = {
      page: this.data.page
    };
    if (this.data.invoiceType == "-1") {
      // 发票抬头
      urlStr = app.getNetAddresss("plugin.invoice.frontend.rise.get-list");
    } else if (this.data.invoiceType == "3") {
      // 申请开票
      urlStr = app.getNetAddresss("plugin.invoice.frontend.index.not-invoice");
    } else if (this.data.invoiceType == "5") {
      urlStr = app.getNetAddresss("plugin.balance-invoice.frontend.balance-invoice.list");
    } else {
      // 已开票、申请中
      urlStr = app.getNetAddresss("plugin.invoice.frontend.index.index");
      requestJson.status = this.data.invoiceType;
    }
    this.setData({
      loading: true
    });
    app._getNetWork({
      url: urlStr,
      data: requestJson,
      success: ({
        data: res
      }) => {
        if (res.result != 1) {
          return app.tips(res.msg);
        }
        let resData = res.data;
        if (this.data.page >= resData.last_page || resData.data.length < resData.pre_page) {
          this.setData({
            finshed: true
          });
        }

        this.setChildData(resData);
        this.data.balance_invoice = wx.getStorageSync('yz_basic_info')?.plugin_setting?.balance_invoice?.is_open || 0;
        this.setData({
          page: this.data.page + 1,
          total: resData.total,
          balance_invoice: this.data.balance_invoice
        });

      },
      complete: () => {
        this.setData({
          loading: false
        })
      }
    })
  },

  // 设置子组件数据
  setChildData(data) {
    console.log(data)
    let invoiceType = this.data.invoiceType;
    if (invoiceType == "-1") {
      this.setData({
        bearerList: this.data.bearerList.concat(data.data)
      })
    } else if (invoiceType == "3") {
      // 后端没有返回 手动添加开发票按钮
      data.data.forEach(item => {
        item.button_models = [{
          name: "申请开票",
          value: "apply_invoice",
          api: "test",
          type: ""
        }]
      })
      this.setData({
        orderList: this.data.orderList.concat(data.data),
        sinvoice: data.sinvoice
      })
    } else {
      this.setData({
        invoiceList: this.data.invoiceList.concat(data.data)
      })
    }
  },

  refreshComponent() {
    this.setData({
      loading: false,
      finshed: false,
      page: 1,
      total: 0,
      invoiceList: [],
      orderList: [],
      bearerList: [],
    })
    this.getListData();
  },

  onReachBottom() {
    this.getListData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})